package books.ThinkingInJava.src.Chapter12;

import books.ThinkingInJava.src.Tools.Output;

public class TestException {
    public static void main(String[] args) {
        TestException te = new TestException();
        try {
            te.well();
//        } catch (Exception si) {

        } catch (Exception e) {
            Output.println("What");
        } finally {
            Output.println("finally always works.");
        }

        try {
            if (1 == 2) {
//                throw new LoggingException();
            }
            if (2 != 2) {
//                throw new SimpleException("My exception");
            }
//            throw new Exception("My exception");
//        } catch (LoginException lo) {
//            lo.printStackTrace();
//        } catch (SimpleException se) {
//            se.printStackTrace();
        } catch (Exception e) {
//            Output.println(e.getCause());
////            Output.println(e.getMessage());
////            Output.println(e.getLocalizedMessage());
////            e.printStackTrace();
            for (StackTraceElement ste :
                    e.getStackTrace()) {
                Output.println("ClassName:  " + ste.getClassName());
                Output.println("MethodName:  " + ste.getMethodName());
                Output.println("LineNumber:  " + ste.getLineNumber());
            }
        } finally {
            Output.println("Exception done."); //任何情况下，finally里的程序都可以运行
        }
    }

    public void well() {
//        throw new SimpleException("hi");
//        throw new IOException();
    }

    /**
     * Test if you can throws a exception without throws declaration.
     * Answer is "Unexpected token".
     */
    public void nope() {
        //e
//        throws new Exception();
    }
}
